package com.myjob.common;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
	
	public Camera mCamera;
	public SurfaceHolder mHolder;

	public MySurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mHolder = getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mCamera = Camera.open();
		
		try {
			mCamera.setPreviewDisplay(mHolder);
		} catch (Exception e) {
			mCamera.release();
			mCamera = null;
			Log.e(MyConfig.DBG_LOG, "______ Camera preview fail : "+e.toString());
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		try{
			
			if(mCamera != null){
				Camera.Parameters cParams = mCamera.getParameters();
				cParams.setPreviewSize(width, height);
				mCamera.setParameters(cParams);
				mCamera.startPreview();
			} else {
				Log.e(MyConfig.DBG_LOG, "mCamera is null");
			}
			
		} catch (Exception e) {
			Log.e(MyConfig.DBG_LOG, "___surfaceChanged() - "+e.toString());
			e.printStackTrace();
			
			mCamera.stopPreview();
			mCamera.release();
			mCamera = null;
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if(mCamera != null){
			mCamera.stopPreview();
			mCamera.release();
			mCamera = null;			
		}
	}

}
